'use strict';

const Controller = require('egg').Controller;

class ScoresController extends Controller {
  async add() { //添加成绩
    const {ctx} = this;
    let {name,theory,skill,stu} = ctx.request.body;
    if(!name || !theory || !skill || !stu){
        ctx.body = {
            code:2,
            mes:'缺少参数'
        }
        return;
    }

    let res = await ctx.service.scores.scores.add(ctx.request.body);
    if(res.affectedRows == 1){
        ctx.body = {
            code:0,
            mes:'成绩添加成功'
        }
    } else {
        ctx.body = {
            code:1,
            mes:'添加失败'
        }
    }
    // ctx.body = res;
  }
  async list(){
      const {ctx} = this;
      let res = await ctx.service.scores.scores.list();
        if(res.length > 0){
            ctx.body = {
                code:0,
                data:res
            }
        } else {
            ctx.body = {
                code:1,
                mes:'查询失败'
            }
        }
    //   ctx.body = res;
  }
  async update(){
    const {ctx} = this;
    let {id,name,theory,skill,stu} = ctx.request.body;
    if(!id || !name || !theory || !skill || !stu){
      ctx.body = {
          code:2,
          mes:'缺少参数'
      }
      return;
  }

  let res = await ctx.service.scores.scores.update(ctx.request.body);
  if(res.affectedRows == 1){
      ctx.body = {
          code:0,
          mes:'编辑成功'
      }
  } else {
      ctx.body = {
          code:1,
          mes:'编辑失败'
      }
  }
}
async del(){
    const {ctx} = this;
    let {id} = ctx.query;
    let res = await ctx.service.scores.scores.del(id);

    if(res.affectedRows > 0){ //删除多个
        ctx.body = {
            code:0,
            mes:'删除成功'
        }
    } else {
        ctx.body = {
            code:1,
            mes:'删除失败'
        }
    }
}
}

module.exports = ScoresController;

